﻿using System;
using TSU.FAMC.ComputerEngineering.Classes.Circuits.Events;

namespace TSU.FAMC.ComputerEngineering.Classes.Circuits.Basis
{
    /// <summary>
    /// A stub element connecting two circuit elements (1 - 1 relation).
    /// </summary>
    public class Link : Element
    {
        // Methods

        /// <summary>
        /// Default contructor.
        /// </summary>
        public Link()
        {
            // contraining input and output interface
            Inputs.OnModified += Inputs_OnModified;
            Outputs.OnModified += Outputs_OnModified;
        }


        // Event handlers

        /// <summary>
        /// OnModified event handler for input interface.
        /// </summary>
        /// <param name="sender">Sender object.</param>
        /// <param name="e">Event arguments.</param>
        private void Outputs_OnModified(object sender, CircuitInterfaceEventArgs e)
        {
            if (Outputs.Count > 1)
            {
                throw new InvalidOperationException("Link element has to have only one output. Use Joint element to n-m relation.");
            }
        }

        /// <summary>
        /// OnModified event handler for output interface.
        /// </summary>
        /// <param name="sender">Sender object.</param>
        /// <param name="e">Event arguments.</param>
        private void Inputs_OnModified(object sender, CircuitInterfaceEventArgs e)
        {
            if (Inputs.Count > 1)
            {
                throw new InvalidOperationException("Link element has to have only one input. Use Joint element to n-m relation.");
            }            
        }

        /// <summary>
        /// Clones link element.
        /// </summary>
        /// <returns>Cloned link element.</returns>
        public override object Clone()
        {
            return new Link
            {
                Delay = Delay,
                Id = Id,
                Name = Name
            };
        }
    }
}
